﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class AddUnitTypeViewModel:ViewModelBase
    {
        public AppData AppData { get; set; } = AppData.Instance;
        private UnitType unitType = new UnitType();
        /// <summary>
        /// 当前单位类型
        /// </summary>
        public UnitType UnitType

        {
            get { return unitType; }
            set { unitType= value; RaisePropertyChanged(nameof(UnitType)); }
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> CloseUnitCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    arg.Close();
                });
            }
        }
        /// <summary>
        /// 添加新的物资类型
        /// </summary>
        public RelayCommand<Window> AddUnitTypeCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (string.IsNullOrEmpty(unitType.Name)) return;
                    unitType.InsertDate = DateTime.Now;
                    unitType.MenberId = AppData.CurrentUser.Id;
                    unitType.MenberName = AppData.CurrentUser.Name;
                    var count = new UnitTypeProvider().Insert(unitType);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else
                    {
                        arg.Close();
                    }
                });
            }
        }
    }
}
